#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : lee

import sys
import os
import logging
import re
from SceneCheckTool.checkScript.CheckBase import CheckBase
from elsLibs.ProjectInfo import ProjectInfo

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CfxPgYetiSurfaceInputCacheCheck(CheckBase):
    def __init__(self):
        super(CfxPgYetiSurfaceInputCacheCheck, self).__init__()
        self.init_els()

    def init_els(self):
        self.els_project = ProjectInfo()

        # 通过文件名称获取资产信息
        maya_file = mu.sceneName()
        file_name = os.path.basename(maya_file)
        self.asset_short_type = file_name.split("_")[0]
        self.asset_name = file_name.split("_")[1]
        self.asset_type = self.els_project.asset_short_type_data[self.asset_short_type]

    def check_pgYeti_cache_attrs(self):
        """
        描述：pgYeti节点缓存检查的启动函数
        """
        error_nodes = []
        for yeti_node in cmds.ls(type="pgYetiMaya"):# 检查节点缓存路径是否匹配
            if not self.check_pyYeti_input_cache(yeti_node):
                error_nodes.append(yeti_node)
            else:
                if cmds.getAttr(yeti_node + ".fileMode") == 0:
                    error_nodes.append(yeti_node)

        return error_nodes

    def check_pyYeti_input_cache(self, node):
        input_cache_file = cmds.getAttr(node + ".cacheFileName")
        if not input_cache_file:
            return False
        if os.path.normpath(input_cache_file) != os.path.normpath(self.get_input_cache_path(node)):
            return False
        if not os.path.exists(input_cache_file):
            return None
        return True

    def get_input_cache_path(self, node):
        cache_folder_path = os.path.join(self.els_project.root_path, "Assets", self.asset_type, self.asset_name, "hair", "staticYetiFur")
        fur_file_name = "{0}.fur".format(cmds.listRelatives(node, p=1)[0])
        cache_fur_file = os.path.join(cache_folder_path, fur_file_name)
        return cache_fur_file

    def check(self):
        result_proc = self.check_pgYeti_cache_attrs()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        try:
            for yeti_node in cmds.ls(type="pgYetiMaya"):# 检查节点缓存路径是否匹配
                if self.check_pyYeti_input_cache(yeti_node) is False:
                    cmds.setAttr(yeti_node + ".cacheFileName", self.get_input_cache_path(yeti_node), type="string")
                    cmds.setAttr(yeti_node + ".fileMode", 1)
                elif self.check_pyYeti_input_cache(yeti_node) is not None:
                    if cmds.getAttr(yeti_node + ".fileMode") == 0:
                        cmds.setAttr(yeti_node + ".fileMode", 1)
            return u'Succeed:没有问题\n'
        except:
            pgYeti_nodes = self.check_pgYeti_cache_attrs()
            result_log = u'Error:%s\n修复失败' % pgYeti_nodes
            return result_log
